import time

from selenium import webdriver
from selenium.webdriver.common.by import By


class BrowserOperation:
    def __init__(self):
        self.driver = webdriver.Chrome("C:\Program Files\Google\Chrome\Application\chromedriver.exe")
        self.driver.get('https://www.baidu.com')

    # 设置浏览器窗口大小
    def window_size(self):
        for i in range(3):
            self.driver.set_window_size(480, 800)
            time.sleep(2)
            self.driver.maximize_window()
            time.sleep(2)
            self.driver.minimize_window()
            time.sleep(2)

    # 浏览器页面的前进与后退
    def forward_back(self):
        for i in range(3):
            # 后退
            self.driver.back()
            time.sleep(1)
            # 前进
            self.driver.forward()
            time.sleep(1)

    # 浏览器的刷新
    def refresh(self):
        time.sleep(2)
        self.driver.refresh()
        time.sleep(2)

    # 浏览器的退出
    def quit(self, seconds):
        time.sleep(seconds)
        self.driver.quit()

    # 浏览器的关闭
    def close(self):
        time.sleep(2)
        # 根据链接名字来定位 新闻
        news = self.driver.find_element(By.LINK_TEXT, '新闻')
        print(news)
        # 点击新闻标签
        news.click()
        # 关闭当前页面
        time.sleep(2)
        self.driver.close()
        time.sleep(2)


if __name__ == '__main__':
    bo = BrowserOperation()
    # bo.window_size()
    # bo.forward_back()
    # bo.refresh()
    # bo.quit(2)
    bo.close()
